package com.aaa.fpa.service;

import com.aaa.fpa.dao.ResultDao;
import com.aaa.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ResultServiceimpl implements ResultService {
    @Autowired
    private ResultDao resultDao;
    @Override
    public ResultBean findAllrt(Page page, Result result){
        PageHelper.startPage(page.getPage(),page.getLimit());
        List<Result> list = resultDao.findAllrt(result);
        PageInfo<Result> pageInfo =new PageInfo<Result>(list);
        return new ResultBean(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }
    @Override
    public int deletesrt(@Param("tester_id") int[ ] tester_id){

        return resultDao.deletesrt(tester_id);
    }
    @Override
    public Map<String,Integer> findAllfpa() {
        List<Result> results = resultDao.findAllfpa();
        int r = 0;
        int y = 0;
        int b =0;
        int g = 0;
        int f =0;
        Iterator iterator = results.iterator();
        while(iterator.hasNext()){
            Result result = (Result)  iterator.next();
            double red = result.getRed_count();
            double yellow = result.getYellow_count();
            double blue = result.getBlue_count();
            double green = result.getGreen_count();
            if ( red > yellow && red > blue && red > green){
                ++r;
            } else if (yellow>red && yellow>blue && yellow>green) {
                ++y;
            } else if (blue>red && blue>yellow && blue>green) {
                ++b;
            } else if (green>red && green>yellow && green>blue) {
                ++g;
            } else  {
                        ++f;
            }
        }
        Map<String,Integer> map =new HashMap();
        map.put("r",r);
        map.put("y",y);
        map.put("b",b);
        map.put("g",g);
        map.put("f",f);
        return  map;
    }

    @Override
    public Result check(int tester_id) {

        return resultDao.check(tester_id);
    }

    @Override
    public Div checkdiv(int id) {
        System.out.println(id);
        return resultDao.checkdiv(id);
    }

}
